<?php
namespace Shoptaepc\Model;
use Think\Model;
class EventsModel extends Model {
	public $sid;
	public function getCurEvent(){
		$wh["sid"]=$this->sid;
		$wh["start_time"]=array('lt',date("Y-m-d H:i:s"));
		$wh["end_time"]=array('egt',date("Y-m-d H:i:s"));
		$fields = "id,id,none_anim,taobao_user_id,curtool,lotts,wins,range_time,start_time,end_time,order_payment,order_success_times,order_pay_times,front_pic,front_pic_url,front_cfg,deadline,brandid,orderset,cart_items,tms_cartadd,cart_items_pics";
		$fields = "id,id,none_anim,taobao_user_id,curtool,lotts,wins,range_time,start_time,end_time,order_payment,order_success_times,order_pay_times,front_pic,front_pic_url,front_cfg,deadline,brandid,orderset,cart_items,tms_cartadd,cart_items_pics,tms_newcomer,tms_everyday,tms_cartadd,tms_cartadd_limit,orderset,andor,order_pay_time_start,order_pay_time_end,order_payment,cart_items,order_pay_times,order_success_times,tms_goodrate,lotts,wins";
		
		$event = $this->where($wh)->getField($fields);
		if(!$event){
			$event = M("ShopCfg")->where(Array("sid"=>$this->sid))->getField($fields);
			$daily=true;
		}
		if(!$event){
			$this->hasEvent=false;
			return;
		}

		$event = current($event);
		if($daily){
			$event["daily"] = $daily;
			$event["id"]=0; //日常活动的eid为0
		}
		if(strtotime($event["start_time"])>time()){
			$countdown = strtotime($event["start_time"])-time();
		}else{
			$countdown = strtotime($event["end_time"])-time();
		}
		
				
		$GFT = M("EventsGifts"); //当前活动的奖品列表
		$gifts = $GFT->where(Array("event_id"=>$event["id"]))
		             ->group("gift_id")->order("`level` ASC")
					 ->getField("`id`,`level`,`type`,`price`, `level_name`, `status`, `name`, `isshow`, `quantity`, `pic_url`");
		$gift_type = C("GFT_TYPE");
		foreach($gifts as $k=>$gift){
			foreach($gift_type as $tp){
				if($gift["type"]==$tp["id"]){
					$gifts[$k]["type"]=$tp["name"];
				}
			}
		}
		$event["cart_items_count"]=count(json_decode($event["cart_items"]));
		$this->hasEvent=true;
		$this->cfg=$event;
		$this->gifts=$gifts;
		$this->countdown=$countdown;
	}
}